package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.pojo.SystemAnnouncement;
import com.example.stuwork.service.SystemAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统公告控制器
 */
@RestController
@RequestMapping("/announcement")
public class SystemAnnouncementController {

    @Autowired
    private SystemAnnouncementService systemAnnouncementService;
    
    /**
     * 获取所有系统公告
     *
     * @return 系统公告列表
     */
    @GetMapping
    public Result<List<SystemAnnouncement>> getAllAnnouncements() {
        List<SystemAnnouncement> announcements = systemAnnouncementService.findAll();
        return Result.success("获取公告列表成功", announcements);
    }
    
    /**
     * 根据ID获取系统公告
     *
     * @param announcementId 公告ID
     * @return 系统公告
     */
    @GetMapping("/{announcementId}")
    public Result<SystemAnnouncement> getAnnouncementById(@PathVariable Long announcementId) {
        SystemAnnouncement announcement = systemAnnouncementService.getById(announcementId);
        if (announcement != null) {
            return Result.success("获取公告详情成功", announcement);
        } else {
            return Result.error("公告不存在");
        }
    }
    
    /**
     * 添加系统公告
     *
     * @param announcement 系统公告信息
     * @return 添加结果
     */
    @PostMapping
    public Result<SystemAnnouncement> addAnnouncement(@RequestBody SystemAnnouncement announcement) {
        // 参数校验
        if (announcement == null) {
            return Result.error("公告信息不能为空");
        }
        
        if (announcement.getTitle() == null || announcement.getTitle().trim().isEmpty()) {
            return Result.error("公告标题不能为空");
        }
        
        if (announcement.getContent() == null || announcement.getContent().trim().isEmpty()) {
            return Result.error("公告内容不能为空");
        }
        
        boolean success = systemAnnouncementService.addAnnouncement(announcement);
        if (success) {
            return Result.success("添加公告成功", announcement);
        } else {
            return Result.error("添加公告失败");
        }
    }
    
    /**
     * 更新系统公告
     *
     * @param announcementId 公告ID
     * @param announcement 系统公告信息
     * @return 更新结果
     */
    @PutMapping("/{announcementId}")
    public Result<SystemAnnouncement> updateAnnouncement(
            @PathVariable Long announcementId,
            @RequestBody SystemAnnouncement announcement) {
        // 参数校验
        if (announcement == null) {
            return Result.error("公告信息不能为空");
        }
        
        // 确保路径中的ID与请求体一致
        announcement.setAnnouncementId(announcementId);
        
        if (announcement.getTitle() == null || announcement.getTitle().trim().isEmpty()) {
            return Result.error("公告标题不能为空");
        }
        
        if (announcement.getContent() == null || announcement.getContent().trim().isEmpty()) {
            return Result.error("公告内容不能为空");
        }
        
        // 检查公告是否存在
        SystemAnnouncement existingAnnouncement = systemAnnouncementService.getById(announcementId);
        if (existingAnnouncement == null) {
            return Result.error("公告不存在");
        }
        
        boolean success = systemAnnouncementService.updateAnnouncement(announcement);
        if (success) {
            SystemAnnouncement updatedAnnouncement = systemAnnouncementService.getById(announcementId);
            return Result.success("更新公告成功", updatedAnnouncement);
        } else {
            return Result.error("更新公告失败");
        }
    }
    
    /**
     * 删除系统公告
     *
     * @param announcementId 公告ID
     * @return 删除结果
     */
    @DeleteMapping("/{announcementId}")
    public Result<Boolean> deleteAnnouncement(@PathVariable Long announcementId) {
        // 检查公告是否存在
        SystemAnnouncement existingAnnouncement = systemAnnouncementService.getById(announcementId);
        if (existingAnnouncement == null) {
            return Result.error("公告不存在");
        }
        
        boolean success = systemAnnouncementService.deleteAnnouncement(announcementId);
        if (success) {
            return Result.success("删除公告成功", true);
        } else {
            return Result.error("删除公告失败");
        }
    }
} 